//
//  GRWebBrower.m
//  GroupCost_IOS
//
//  Created by apple on 2019/2/21.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "GRWebBrower.h"
#import <UIKit/UIKit.h>
#import "GRWebViewController.h"

@implementation GRWebBrower

+ (void)openURL:(NSString *)url{
	//本地网页
	GRWebViewController *webController;
	if ([url hasPrefix:@"html://"]) {
		NSString *fileName = [url stringByReplacingOccurrencesOfString:@"html://" withString:@""];
		webController = [[GRWebViewController alloc] initWithFileName:fileName];
	}else if ([url hasPrefix:@"https://"] || [url hasPrefix:@"http://"]){
		webController = [[GRWebViewController alloc] initWithURLStr:url];
	}
	[[self getCurrentNavgationController] pushViewController:webController animated:YES];
}

+ (void)openURL:(NSString *)url hiddenBar:(BOOL)isHidden{
	//本地网页
	GRWebViewController *webController;
	if ([url hasPrefix:@"html://"]) {
		NSString *fileName = [url stringByReplacingOccurrencesOfString:@"html://" withString:@""];
		webController = [[GRWebViewController alloc] initWithFileName:fileName];
	}else if ([url hasPrefix:@"https://"] || [url hasPrefix:@"http://"]){
		webController = [[GRWebViewController alloc] initWithURLStr:url];
	}
	UINavigationController *navagationVC = [self getCurrentNavgationController];
	[navagationVC setNavigationBarHidden:isHidden];
	[navagationVC pushViewController:webController animated:YES];
}

+ (void)closeBrower{
	[[self getCurrentNavgationController] popViewControllerAnimated:YES];
}

+ (void)addSuviewWebBrower:(UIView *)view openURL:(NSString *)url{
	GRWebViewController *webController = [[GRWebViewController alloc] initWithURLStr:url];
	[view addSubview:webController.view];
}

+ (UIViewController *)getCurrentRootViewController {
	UIViewController *result;
	// Try to find the root view controller programmically
	// Find the top window (that is not an alert view or other window)
	
	UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
	if (topWindow.windowLevel != UIWindowLevelNormal){
		NSArray *windows = [[UIApplication sharedApplication] windows];
		for(topWindow in windows){
			if (topWindow.windowLevel == UIWindowLevelNormal)
				break;
		}
	}
	UIView *rootView = [[topWindow subviews] objectAtIndex:0];
	id nextResponder = [rootView nextResponder];
	
	if ([nextResponder isKindOfClass:[UIViewController class]]){
		if ([nextResponder presentedViewController]) {
			result = [nextResponder presentedViewController];
		}else if ([nextResponder isKindOfClass:[UITabBarController class]]){
			result = [nextResponder selectedViewController];
		}else if ([nextResponder isKindOfClass:[UINavigationController class]]){
			result = [(UINavigationController *)nextResponder visibleViewController];
		}else{
			result = nextResponder;
		}
	}else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil){
		result = topWindow.rootViewController;
	}else{
		NSAssert(NO, @"ShareKit: Could not find a root view controller.  You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
	}
	return result;
}

+ (UINavigationController *)getCurrentNavgationController{
	UIViewController *topViewController = [self getCurrentRootViewController];
	if ([topViewController isKindOfClass:UITabBarController.class]) {
		UITabBarController *tabbarVC = (UITabBarController *)topViewController;
		return tabbarVC.selectedViewController;
	} else {
		UINavigationController *navagationVC = (UINavigationController *)topViewController;
		return navagationVC;
	}
}

@end
